/**
 * 
 */
package kr.co.insoft.core.service.configration.connection.impl;

import java.util.List;

import kr.co.insoft.auth.model.security.User;
import kr.co.insoft.core.model.configration.connection.ConnectionEntityModel;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.repository.configration.connection.ConnectionRepository;
import kr.co.insoft.core.service.configration.connection.ConnectionService;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author skpark
 * 
 */
@Service
public class ConnectionServiceImpl implements ConnectionService {

	@Autowired
	ConnectionRepository connectionRepository;

	private static final Logger logger = LoggerFactory
			.getLogger(ConnectionServiceImpl.class);

	@Override
	public PagingModel getLists(PagingModel pagingModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();
			pagingModel.setSearchOrCondition(user.getQueryAuthParam());

			List<ConnectionEntityModel> lists = connectionRepository
					.getLists(pagingModel);
			int totalCount = connectionRepository.getTotalCount(pagingModel);

			logger.info("connection param : {}",
					pagingModel.getSearchAndCondition());

			return new PagingModel(lists, totalCount, pagingModel);
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void createConnection(ConnectionEntityModel connectionEntityModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			connectionEntityModel.setAuth((User) auths.getPrincipal());

			/* Conneciton 등록 */
			connectionRepository.createConnection(connectionEntityModel);
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}

	}

	@Override
	public ConnectionEntityModel getArticle(String connectionid)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();

			ConnectionEntityModel connectionEntityModel = new ConnectionEntityModel();
			connectionEntityModel.setConnectionid(connectionid);
			connectionEntityModel.setAuth((User) auths.getPrincipal());
			return connectionRepository.getConnection(connectionEntityModel);
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public void removeConnection(ConnectionEntityModel connectionEntityModel)
			throws ServiceException {
		try {
			connectionRepository.removeConnection(connectionEntityModel);
		} catch (Exception e) {
			logger.error(ExceptionCode.SERVICE_EXCEPTION, e);
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public List<ConnectionEntityModel> getAllLists() throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();

			ConnectionEntityModel connectionEntityModel = new ConnectionEntityModel();
			connectionEntityModel.setAuth((User) auths.getPrincipal());
			return connectionRepository.getAllLists(connectionEntityModel);
		} catch (Exception e) {
			logger.error(ExceptionCode.SERVICE_EXCEPTION, e);
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
}
